package com.example.linyiquan.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.linyiquan.app.R;
import com.example.linyiquan.app.util.MyVolley;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by linyiquan on 17-4-15.
 */

public class RegisterActivity extends Activity implements View.OnClickListener {

    private EditText et_username;
    private EditText et_password;
    private EditText again_password;
    private Button bt_register;
    private SimpleDateFormat format;

    private String username;
    private String password;
    private String password01;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);

        initView();
    }

    /**
     * 添加视图
     */
    private void initView() {
        et_username = (EditText) findViewById(R.id.username);
        et_password = (EditText) findViewById(R.id.password);
        again_password = (EditText) findViewById(R.id.again_register);
        bt_register = (Button) findViewById(R.id.bt_register);
        bt_register.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        getData();
    }


    /**
     * 用get方法进行数据提交
     */
    private void getData() {

        username = et_username.getText().toString();
        password = et_password.getText().toString();
        password01 = again_password.getText().toString();


        MyVolley.init(this);
        String url = "http://120.76.96.34/weizhanjsp/user/register?&sex=0&city=厦门";

        url += "&username=" + username;
        url += "&password=" + password;
        url += "&birthday=" + System.currentTimeMillis();
        url += "&clientType=" + 1;

        MyVolley.get(url, null, new MyVolley.OnResultListener() {
            @Override
            public void done(JSONObject result) {
                System.out.println("result" + result.toString());
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!password01.equals(password)) {
                    Toast.makeText(RegisterActivity.this, "验证密码不匹配", Toast.LENGTH_SHORT).show();
                }

                int resultCode;
                try {
                    resultCode = result.getInt("resultCode");
                    if (resultCode == 0) {
                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void fail(VolleyError error) {
                System.out.println(error);
            }


        });
    }
}
